package ldkxingzhe.ldkxingzhedemo.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by liangdengke on 2015/8/17.
 */
public class ServiceDemoActivity extends Activity{

    private StartServiceService mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_demo);
    }

    public void onClickExplicitStartService(View view){
        Intent intent = new Intent(this, StartServiceService.class);
        startService(intent);
    }

    public void onClickExplicitEndService(View view){
        stopService(new Intent(this, StartServiceService.class));
    }

    public void onClickBindService(View view){
        Intent bindIntent = new Intent(this,StartServiceService.class);
        bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    public void onClickStartIntentService(View view){
        Intent intent = new Intent(this, IntentServiceService.class);
        startService(intent);
    }

    public void onClickUnBindService(View view){
        unbindService(mServiceConnection);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("TAG","onServiceConnected");
            mService = ((StartServiceService.MyBinder)service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("TAG","onServiceDisconnected");
            mService = null;
        }
    };
}
